package org.wicketstuff.viewstack.model;

import java.io.Serializable;
import java.util.ArrayList;

import org.apache.wicket.WicketRuntimeException;
import org.wicketstuff.viewstack.view.IView;

/**
 *
 * @author fernando wermus
 */
public class ViewStackModel implements  IViewStackModel, Serializable{

	private static final long serialVersionUID = 1L;
	public static final String ACTIVE_VIEW = "activeView";
	protected ArrayList<IView> items;
	private IView activeView;


	/**
	 * 
	 */
	public ViewStackModel()
	{	
		items = new ArrayList<IView>();
	}

	public void add(int index, IView item)
	{
		items.add(index, item);
	}

	public IView getView(int index)
	{
		return items.get(index);
	}
	public void setActiveView(IView activeView) {
		if (items.contains(activeView))
		{
			this.activeView = activeView;
		} else
		{
			throw new WicketRuntimeException("Attempt to set a default view that is not contained in the model");
		}
	}

	public Integer size()
	{
		return items.size();
	}

	public IView getObject() {
		return getActiveView();
	}
	public void setObject(IView object) {
		throw new UnsupportedOperationException();
	}

	public IView getActiveView() {
		return activeView;
	}
	
	public void detach() {
		
	}

	public void add(IView view) {
		items.add(view);
	}

	public void setObject(Object object) {
		// TODO Auto-generated method stub
		
	}

	public static ViewStackModel empty() {
		return new ViewStackModel();
	}

	public Integer getActiveViewIndex() {
		return items.indexOf(getActiveView());
		
	}

}
